package com.stox.shared.watchlist.model;

import java.util.List;

import com.stox.shared.core.model.AbstractModel;
import com.stox.shared.core.util.StringUtil;

public class Watchlist extends AbstractModel {

	private transient List<WatchlistEntry> entries;
	
	public Watchlist() {
		
	}
	
	public Watchlist(String text) {
		String[] tokens = text.split(",");
		if(0 < tokens.length && StringUtil.hasText(tokens[0])) setId(Long.parseLong(tokens[0]));
		if(1 < tokens.length) setName(tokens[1]);
	}
	
	@Override
	public String toString() {
		return getId()+","+getName();
	}
	
	/**
	 * Watchlist is a mutable object, name being editable field.
	 * Thus it should not be considered for computation of hashcode.
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		Long id = getId();
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass().isAssignableFrom(obj.getClass()))
			return false;
		AbstractModel other = (AbstractModel) obj;
		Long id = getId();
		if (id == null) {
			if (other.getId() != null)
				return false;
		} else if (!id.equals(other.getId()))
			return false;
		return true;
	}
	
	public List<WatchlistEntry> getEntries() {
		return entries;
	}
	
	public void setEntries(List<WatchlistEntry> entries) {
		this.entries = entries;
	}
	
}
